import sys

from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout, QApplication, QHeaderView, QCheckBox, QTableWidgetItem
from qfluentwidgets import CardWidget, PushButton, SearchLineEdit, TableWidget,setCustomStyleSheet
#这个项目很多模块,只要能从qfluentwidgets中导入,就从qfluentwidget中导入
from test1.student_system.database.student_db import StudentDB
from test1.student_system.student.student_dialog import BaseStudentDialog, AddStudentDialog
from test1.student_system.util.custom_style import ADD_BUTTON_STYLE, BATCH_DELETE_BUTTON_STYLE
#这时从自定义的样式文件中导入需要的组件样式

class StudentInterface(QWidget):
    def __init__(self):
        super().__init__()
        self.resize(800, 600)
        self.layout = QVBoxLayout(self)
        self.setObjectName("studentInterface")
        self.students = []#初始化一个空列表,放着以后用
        self.setup_ui()
        self.load_data ()
        self.populate_table()

        #top button
        #self.card_widget=CardWidget(self)#按钮父元素,卡片样式
        #buttons_layout=QHBoxLayout(self.card_widget)#
        #self.layout = QVBoxLayout(self)#添加布局

        # self.addButton=PushButton("新增",self)
        # self.searchInput=SearchLineEdit(self)
        # self.searchInput.setPlaceholderText("搜索学生姓名或学号......")
        # self.searchInput.setFixedWidth(500)
        # self.batchDeleteButton=PushButton("批量删除",self)
        #
        # #buttons_layout.addWidget(self.addButton)
        # #buttons_layout.addWidget(self.searchInput)
        # #buttons_layout.addWidget(self.batchDeleteButton)
        # #layout.addWidget(card_widget)
        #
        # self.setStyleSheet("StudentInterface {background: }")#这就是qss样式设置方式
        # self.resize(1280,760)
        #
        # self.addButton=PushButton('添加学生')
        # #layout = QVBoxLayout(self)
        #
        # #top button
        # self.card_widget=CardWidget(self)#按钮父元素,卡片样式
        # self.layout = QVBoxLayout(self.card_widget)
        # self.buttons_layout=QHBoxLayout()#它归属于card_widget
        # self.buttons_layout.addWidget(self.addButton)
        #self.layout.addWidget(self.card_widget,alignment=Qt.AlignmentFlag.AlignTop)
    def setup_ui(self):
        #
        #top button
        card_widget=CardWidget(self)#按钮父元素,卡片样式
        #设置卡片的宽度,宽度继承块的宽度
        #card_widget.setFixedWidth(self.width())
        #设置卡片的高度
        #card_widget.setFixedHeight(100)
        # 设置卡片背景颜色为红色,圆角
        card_widget.setStyleSheet("background-color: pink; border-radius: 8px;")

        # 创建自适应布局
        buttons_layout=QHBoxLayout(card_widget)#它归属于card_widget

        self.addButton=PushButton("新增",self)
        #self.addButton.setFixedWidth(100)
        setCustomStyleSheet(self.addButton,ADD_BUTTON_STYLE,ADD_BUTTON_STYLE)
        #这个方法需要3个参数,第一个参数是按钮对象,第二个参数是白色主题样式,第三个参数是黑色主题样式,这里我设置的前后都一样
        #给add按钮添加事件
        self.addButton.clicked.connect(self.add_student)
        self.searchInput=SearchLineEdit(self)
        self.searchInput.setPlaceholderText("搜索学生姓名或学号......")
        self.searchInput.setFixedWidth(500)
        self.batchDeleteButton=PushButton("批量删除",self)
        #self.batchDeleteButton.setFixedWidth(100)
        setCustomStyleSheet(self.batchDeleteButton,BATCH_DELETE_BUTTON_STYLE,BATCH_DELETE_BUTTON_STYLE)
        #这个方法需要3个参数,第一个参数是按钮对象,第二个参数是白色主题样式,第三个参数是黑色主题样式,这里我设置的前后都一样

        buttons_layout.addWidget(self.addButton)
        buttons_layout.addWidget(self.searchInput)
        #buttons_layout.addStretch(1)#添加一个占位符,这个占位符可以把空气全部占为己有,让元素向右靠齐
        buttons_layout.addWidget(self.batchDeleteButton)

        self.layout.addWidget(card_widget)#对象建立时,建立的垂直布局 #把卡片添加到垂直布局中
        #这样卡片上下居中;卡片里的元素左右居中,因为元素被button_layout管理

        #卡片设置完了,接下来设置表格
        self.table_widget=TableWidget(self)
        #把表格加入垂直布局中
        self.layout.addWidget(self.table_widget)
        #需要设置表格的圆角
        self.table_widget.setStyleSheet("border-radius: 8px;")
        self.table_widget.setBorderVisible( True)#边框可见
        #设置一下表格的宽度是父元素的宽度
        #self.table_widget.setFixedWidth(self.width())
        #设置表格的高度:300
        self.table_widget.setFixedHeight(300)
        #设置表格的背景颜色:浅绿色
        self.table_widget.setStyleSheet("background-color: lightgreen;")
        #设置表格的列数
        self.table_widget.setColumnCount(11)
        #设置表头
        self.table_widget.setHorizontalHeaderLabels(["","学号ID","姓名","学号","性别","班级","语文","数学","英语","总分","操作"])
        #让表头填充满
        self.table_widget.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeMode.Stretch)

        self.setStyleSheet("StudentInterface {background: white}")#这就是qss样式设置方式
    def load_data(self):
        #self.students=[
        #    {"student_id":1,"student_name":"张三","student_number":"202410101","gender":1,"class_name":"一年1班","chinese_score":90,"math_score":80,"english_score":70,"total_score":240},
        #    {"student_id":2,"student_name":"李四","student_number":"202410102","gender":2,"class_name":"一年1班","chinese_score":80,"math_score":90,"english_score":80,"total_score":270},
        #    {"student_id":3,"student_name":"王五","student_number":"202410103","gender":1,"class_name":"一年1班","chinese_score":70,"math_score":80,"english_score":90,"total_score":250},
        #    {"student_id":4,"student_name":"赵六","student_number":"202410104","gender":2,"class_name":"一年1班","chinese_score":60,"math_score":70,"english_score":80,"total_score":230},
        #    {"student_id":5,"student_name":"孙七","student_number":"202410105","gender":1,"class_name":"一年1班","chinese_score":50,"math_score":60,"english_score":70,"total_score":190},
        #]
        with StudentDB() as db:
            self.students=db.fetch_students()

    #展示表格信息
    def populate_table(self):
        self.table_widget.setRowCount(len(self.students))#根据学生的多少设置行数
        for row,student_info in enumerate(self.students):
            self.setup_table_row(row,student_info)#把学生信息添加到表格中
    #把一个学生的信息渲染进一行
    def setup_table_row(self,row,student_info):
        checkbox=QCheckBox()#创建一个单选框
        self.table_widget.setCellWidget(row,0,checkbox)#这一行的0列添加单选框
        #赋值其他列
        for index,field_name in enumerate(["student_id","student_name","student_number",
                                  "gender","class_name","chinese_score","math_score",
                                  "english_score","total_score"]):
            #enumerate是从列表中获取索引和值
            field_value=student_info.get(field_name,"")#这个"",表示没取到时,默认为空
            if field_name=="gender":
                field_value="男" if field_value==1 else "女" if field_value==2 else "未知" #这是2个3元表达式
            #单元格赋值
            item=QTableWidgetItem(str(field_value))#单元格对象
            self.table_widget.setItem(row,index+1,item)#行,列,值 #把单元格对象加入这一行 这一列
    #新增学生的方法
    def add_student(self):
        print(123)
        dialog=AddStudentDialog(self)
        print(1234)
        if dialog.exec():
            print("用户点击了确定按钮")
            # #获取输入的数据
            # student_info=dialog.get_input_data()
            # with StudentDB() as db:
            #     db.insert_student(student_info)
            # self.load_data()
            # self.populate_table()

if __name__=='__main__':
    app=QApplication(sys.argv)
    w=StudentInterface()
    w.show()
    sys.exit(app.exec())



























